//
//  MainViewController.m
//  QQGroup
//
//  Created by cocoazxj@163.com on 15/3/31.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "MainViewController.h"
#import "GroupModel.h"
#import "FriendsModel.h"
#import "HeaderView.h"
#import "FriendTableViewCell.h"

@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate,HeaderViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *tv_main;
@property (nonatomic, strong) NSMutableArray *mArray;
@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

// 懒加载，其实是通过给mArray数组赋值
//- (NSMutableArray *)mArray
//{
//    if(!_mArray)
//    {
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"friends" ofType:@".plist"];
//        NSArray *a = [NSArray arrayWithContentsOfFile:path];
//        NSMutableArray *m = [NSMutableArray array];
//        for(NSDictionary *d in a)
//        {
//            [m addObject:[GroupModel groupModel:d]];
//        }
//        self.mArray = m;
//    }
//    return _mArray;
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 懒加载，什么时候使用什么时候加载;避免在此方法中过多的加载，暂时不需要的东西。
    // cell.friendModel = fm; 等同于 [cell setFriendModel:fm]; 调用Cell类中的setFriendModel方法；
    // 只不过是此法是编译器我们创建的，在.h中外部方法friendModel,setFriendModel,编译器在.m文件中帮我们实现了。
    // 我们要重写setFriendModel时，要写_friendModel = friendModel;只有这样你才能在此类中使用这个Model。
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"friends" ofType:@".plist"];
    NSArray *a = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *m = [NSMutableArray array];
    for(NSDictionary *d in a)
    {
        [m addObject:[GroupModel groupModel:d]];
    }
//    self.mArray = m;
    [self setMArray:m];
//    self.mArray =
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

#pragma mark -
#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.mArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    GroupModel *m = self.mArray[section];
    if(m.isOpen)
    {
        return m.friends.count;
    }
    else
    {
        return 0;
    }
    
}

// 要想使用Header 高度在这里设置。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderView *head = [HeaderView headerView:tableView];
    head.delegate = self;
    GroupModel *gm = self.mArray[section];
    head.groupModel = gm;
    return head;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendTableViewCell *cell = [FriendTableViewCell friendTableViewCell:tableView];
    GroupModel *gm = self.mArray[indexPath.section];
    FriendsModel *fm = gm.friends[indexPath.row];
    NSLog(@"1-------%p------%p",fm,cell.friendModel);
//    cell.friendModel = fm;
    [cell setFriendModel:fm];
    NSLog(@"2-------%p------%p",fm,cell.friendModel);
    NSLog(@"----------------------------%@",[cell friendModel].name);
    return cell;
}

- (void)headerViewDelegate
{
    [self.tv_main reloadData];
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}
@end